<template>
  <div class="setting-user">
    <el-table :data="userList" style="width: 100%">
      <el-table-column prop="user_name" label="姓名"> </el-table-column>
      <el-table-column prop="oa_user_name" label="用户名"> </el-table-column>
      <el-table-column prop="department_name" label="部门"> </el-table-column>
      <el-table-column prop="roles" label="角色">
        <template slot-scope="scope">
          <span>{{ scope.row.roles[0] && scope.row.roles[0].role_name }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="180">
        <template slot-scope="scope">
          <el-button
            type="primary"
            icon="el-icon-edit"
            size="mini"
            circle
            @click="edit(scope.row)"
          ></el-button>
        </template>
      </el-table-column>
    </el-table>
    <div style="float: right; margin-top: 8px">
      <el-pagination
        @current-change="handleCurrentChange"
        :current-page="pageParams.page"
        :page-size="10"
        layout="total, prev, pager, next, jumper"
        :total="totalCount"
      >
      </el-pagination>
    </div>
    <el-dialog :visible.sync="dialogFormVisible" :title="'修改用户角色'" width="400px">
      <el-form ref="form" :model="form" label-width="90px" label-position="right" :rules="rules">
        <el-form-item label="用户名:" prop="oa_user_name">
          <el-input v-model="form.oa_user_name" disabled></el-input>
        </el-form-item>
        <el-form-item label="部门:" prop="department_name">
          <el-input v-model="form.department_name" disabled></el-input>
        </el-form-item>
        <el-form-item label="角色:" prop="roles">
          <el-select v-model="form.roles" placeholder="请选择">
            <el-option
              v-for="item in roleList"
              :key="item.id"
              :label="item.role_name"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="editUser">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
export default {
  name: "settingUser",
  data() {
    return {
      pageParams: {
        page: 1
      },
      totalCount: 0,
      userList: [],
      roleList: [],
      dialogFormVisible: false,
      form: {
        oa_user_name: "",
        id: "",
        roles: []
      }
    };
  },

  methods: {
    editUser() {
      this.$http.user
        .editUser({
          id: this.form.id,
          oa_user_name: this.form.oa_user_name,
          roles: [this.form.roles]
        })
        .then((res) => {
          this.$message({
            message: "修改成功",
            type: "success"
          });
          this.dialogFormVisible = false;
          this.initData();
        });
    },
    edit(row) {
      this.$http.user.getUserById({ id: row.id }).then((res) => {
        this.form = res;
        this.form.roles = this.form.roles[0].id;
        this.dialogFormVisible = true;
      });
    },
    handleCurrentChange(val) {
      this.pageParams.page = val;
      this.initData();
    },
    initData() {
      this.$http.user.getUserList({ ...this.pageParams }).then((res) => {
        this.userList = res.items;
        this.totalCount = res.total;
      });
    }
  },
  watch: {
    dialogFormVisible(val) {
      if (!val) {
        this.form.id = "";
        this.$refs.form.resetFields();
      }
    }
  },
  created() {
    this.initData();
    this.$http.role.getRoleList().then((res) => {
      this.roleList = res;
    });
  }
};
</script>
<style lang="scss" scoped>
</style>

